﻿namespace AnyTap.Models;

/// <summary>
/// 资源调度接口
/// </summary>
public interface IScheduler : IState
{
	/// <summary>
	/// 测试项更新事件
	/// </summary>
	event Action<int> ItemUpdated;

	/// <summary>
	/// 信号更新事件
	/// </summary>
	event Action<SignalStruct> SignalUpdated;
}

/// <summary>
/// 系统状态
/// </summary>
public interface IState
{
	/// <summary>
	/// 状态
	/// </summary>
	StateModel State { get; }

	/// <summary>
	/// 停止
	/// </summary>
	void Stop();

	/// <summary>
	/// 启动测试
	/// </summary>
	void Start();

	/// <summary>
	/// 暂停和恢复
	/// </summary>
	bool Pause(bool pause);

	/// <summary>
	/// 启动测试
	/// </summary>
	void Start(Doc doc);

	/// <summary>
	/// 注销
	/// </summary>
	void Logout();

	/// <summary>
	/// 设置输入信号
	/// </summary>
	void SetSignal(SignalStruct input);

	/// <summary>
	/// 状态改变事件
	/// </summary>
	event Action StateChanged;
}

/// <summary>
/// 系统状态的数据模型
/// </summary>
public class StateModel
{
	private StateInfo info;

	/// <summary>
	/// 当前状态
	/// </summary>
	public Stage Stage { get; private set; }

	/// <summary>
	/// 详细信息
	/// </summary>
	public string Message { get; set; }

	/// <summary>
	/// 详细状态信息
	/// </summary>
	public StateInfo Info
	{
		get => info;
		set
		{
			info = value;
			Stage = value switch
			{
				StateInfo.Stop => Stage.Stop,
				StateInfo.Running => Stage.Running,
				_ => Stage.Ready,
			};
		}
	}
}

/// <summary>
/// 通用状态标志
/// </summary>
public enum Stage
{
	/// <summary>
	/// 忙
	/// </summary>
	Busy,

	/// <summary>
	/// 就绪
	/// </summary>
	Ready,

	/// <summary>
	/// 测试中
	/// </summary>
	Running,

	/// <summary>
	/// 停止,用于通知测试线程终止
	/// </summary>
	Stop,
}

#region 设备信号
/// <summary>
/// 详细状态信息
/// </summary>
public enum StateInfo
{
	/// <summary>
	/// 初始化
	/// </summary>
	Init,

	/// <summary>
	/// 待机
	/// </summary>
	Ready,

	/// <summary>
	/// 收到停止信号
	/// </summary>
	Stop,

	/// <summary>
	/// 测试已停止
	/// </summary>
	Break,

	/// <summary>
	/// 测试
	/// </summary>
	Running,

	/// <summary>
	/// 合格
	/// </summary>
	OK,

	/// <summary>
	/// 不合格
	/// </summary>
	NG,

	/// <summary>
	/// 警告
	/// </summary>
	Warning,

	/// <summary>
	/// 错误
	/// </summary>
	Error,

	/// <summary>
	/// 故障
	/// </summary>
	Fault,
}

/// <summary>
/// 输入信号
/// </summary>
public enum SignalInput
{
	/// <summary>
	/// 无信号
	/// </summary>
	None,

	/// <summary>
	/// 停止
	/// </summary>
	Stop,

	/// <summary>
	/// 启动
	/// </summary>
	Start,
}

/// <summary>
/// 输入信号结构
/// </summary>
public struct SignalStruct
{
	/// <summary>
	/// 输入信号
	/// </summary>
	public SignalInput Input { get; set; }

	/// <summary>
	/// 工位
	/// </summary>
	public int Station { get; set; }
}
#endregion
